import random
# 基础的猜数字游戏环境类
class GuessNumberEnv:
    def __init__(self):
        # 生成 1 到 10 之间的随机数字作为答案
        self.answer = int(random.randint(1, 10))
        # 初始化分数
        self.reward =0;

    def step(self, guess):
        # 根据玩家的猜测给出反馈
        if guess == self.answer:
            self.reward = self.reward+1
            done = True
        elif guess < self.answer:
            self.reward = self.reward-0.1
            done = False
        else:
            self.reward = self.reward-0.1
            done = False
        return self.reward, done


# 自定义包装器类
class GuessCountWrapper:
    def __init__(self, env):
        # 保存被包装的环境
        self.env = env
        # 初始化猜测次数为 0
        self.guess_count = 0

    def step(self, guess):
        # 猜测次数加 1
        guess=int(guess)
        self.guess_count += 1
        # 调用被包装环境的 step 方法
        reward, done = self.env.step(guess)
        # 打印当前猜测次数
        print(f"当前猜测次数: {self.guess_count},是否正确{done},当前得分: {reward}")
        return reward, done


# 使用示例
if __name__ == "__main__":
    # 创建基础环境
    base_env = GuessNumberEnv()
    # 使用包装器包装基础环境
    wrapped_env = GuessCountWrapper(base_env)

    # 重置环境
#    wrapped_env.reset(）
    # 进行猜测
    for _ in range(10):
        # 随机猜测一个数字
        guess = input('请输入您猜测的数：')
        # 执行猜测动作
        reward, done = wrapped_env.step(guess)
        if done:
            print("猜对啦！游戏结束。")
            break
